package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2020/3/12.
 */
@Service
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Override
	public void delete(Long id) {
		//先删除关系
		roleMapper.deleteRelation(id);
		//再删除角色
		roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void save(Role role) {
		roleMapper.insert(role);
	}

	@Override
	public Role get(Long id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> listAll() {
		return roleMapper.selectAll();
	}

	@Override
	public void update(Role role) {
		roleMapper.updateByPrimaryKey(role);
	}

	@Override
	public PageInfo queryForList(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
		List<Role> roles = roleMapper.queryForList(qo);
		return new PageInfo(roles);
	}

	@Override
	public void saveOrUpdate(Role role, Long[] ids) {
		if(role.getId() == null){
			roleMapper.insert(role);
		}else{
			roleMapper.deleteRelation(role.getId());
			roleMapper.updateByPrimaryKey(role);
		}

		if(ids != null){
			for (Long permissionId : ids) {
				roleMapper.saveRelation(role.getId(),permissionId);
			}
		}

	}
}
